package examples;

import helpers.FileHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.urlfetch.FetchOptions;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

public class BlobStore extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String urlString = "http://www.webpagetest.org/result/121015_R9_212/1_waterfall_thumb.png";
		
		byte[] img = null;
		URLFetchService fetchService =
            URLFetchServiceFactory.getURLFetchService();
		// Fetch the image at the location given by the url query string parameter
		URL url = new URL(urlString);
		// Fetch the image at the location given by the url query string parameter
		FetchOptions fetchOptions =FetchOptions.Builder.withDefaults();
		fetchOptions.setDeadline((double)30*60);
		HTTPRequest req = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
        HTTPResponse fetchResponse = fetchService.fetch(req);
        
        String fetchResponseContentType = null;
        for (HTTPHeader header : fetchResponse.getHeaders()) {
            // For each request header, check whether the name equals
            // "Content-Type"; if so, store the value of this header
            // in a member variable
            if (header.getName().equalsIgnoreCase("content-type")) {
                fetchResponseContentType = header.getValue();
                break;
            }
        }
        if (fetchResponseContentType != null) {
        	img = (byte[]) fetchResponse.getContent();
        	/*response.setContentType(fetchResponseContentType);
        	response.getOutputStream().write(img);*/
        	
        	String key = FileHelper.saveToFile(img, fetchResponseContentType, "image.png");
        	
        	BlobKey blobKey = new BlobKey(key);
			BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
			ImagesService imagesService = ImagesServiceFactory.getImagesService();
			System.out.println(imagesService.getServingUrl(blobKey));
			
			/*response.setContentType("text/html");
			response.getWriter().print("aaaa");
			response.getWriter().println(imagesService.getServingUrl(blobKey));*/
        	
        }
	}
}
